Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    login: '登录',
    avatar: 'https://www.prebeauty.work:3001/static/image/photo.png' ,
    nickname: '请先进行登录->',
    
    getvalue:[
      {getvaluetxt:'每日签到领狗粮',getvaluenum:10,getvaluecan:1,getvaluedid:0},
      {getvaluetxt:'每发布一次动态',getvaluenum:20,getvaluecan:5,getvaluedid:0},
      {getvaluetxt:'浏览商城一分钟',getvaluenum:30,getvaluecan:10,getvaluedid:0},
      {getvaluetxt:'购买一次商品',getvaluenum:50,getvaluecan:10,getvaluedid:0},
      {getvaluetxt:'为救助站的萌萌送一次礼物',getvaluenum:100,getvaluecan:2,getvaluedid:0},
      {getvaluetxt:'做一次救助志愿者',getvaluenum:200,getvaluecan:2,getvaluedid:0}
    ]
  },

  //未登录时点击头像提醒登录
  personalData(){
    if(!wx.getStorageSync('token')){
      wx.showModal({
        title:'用户未登录',
        content:'请先进行授权登录',
        success(res){
          if(res.confirm){
            console.log('确定')
          }else if(res.cancel){
            console.log('取消')
          }
        }
      })
    }else{
      wx.navigateTo({
        url: '/wode/personalData/personalData',
      })
    }
  },

  onShow: function(d) {
    this.setData({
      id: wx.getStorageSync('userid'),
      img:wx.getStorageSync('userimg'),
      name:wx.getStorageSync('username'),
    })

    if(wx.getStorageSync('token')){
      this.setData({
        avatar:this.data.img,
        nickname:this.data.name,
        login: '退出'
      })
    }else{
      this.setData({
        avatar:'https://www.prebeauty.work:3001/static/image/photo.png',
        nickname:'请先进行登录->',
        login: '登录'
      })
    }
  },

  bindGetUserInfo:async function(e) {
    //登录
    if(this.data.login === '登录'){

      wx.showLoading({
        title: '登录中',
        duration: 2000
      })

      //获取token和userid
      await new Promise((rv,rj) => {
        wx.login({
          success:res=>{
            console.log(res.code)
            wx.request({
              url: 'https://www.prebeauty.work:3001/api/login?code='+res.code,
              success:r=>{
                try {
                  wx.hideLoading()
                  wx.setStorageSync('token', r.data.token)
                  wx.setStorageSync('userid', r.data.userid)

                  //本地渲染
                  this.setData({
                    avatar: e.detail.userInfo.avatarUrl,
                    nickname: e.detail.userInfo.nickName,
                    login: e.detail.userInfo?'退出':'登录',
                    id:r.data.userid
                  })
                  rv()
                } catch (e) { 
                  console.log(e)
                  wx.showToast({
                    title: '登录失败',
                    icon:'none',
                    duration: 2000
                  })
                }
              },
              fail:err=>{
                rv()
              }
            })
          }
        })
      })
    }
    //退出登录
    else if(this.data.login === '退出'){
      wx.showLoading({
        title: '退出中',
        duration: 2000
      })

      try {
        wx.clearStorageSync()
        this.setData({
          avatar: 'https://www.prebeauty.work:3001/static/image/photo.png' ,
          nickname: '请先进行登录->',
          login: '登录',
        })
      } catch(e) {
        console.log(e)
        wx.showToast({
          title: '退出失败',
          icon:'none',
          duration: 2000
        })
      }  
    }

    //验证是否初次登录
    if(wx.getStorageSync('userid')){
      wx.request({
        url: 'https://www.prebeauty.work:3001/api/userpage',
        header: {
          'content-type': 'application/json'
        },
        method:'GET',
        success:(res)=>{
          console.log(res.data)
          let userlist=res.data
          let users=[]
          for(let i=0;i<userlist.length;i++){
            if(userlist[i].userid==this.data.id){
              console.log(userlist[i])
              
              wx.setStorage({
                data: userlist[i].userimg,
                key: 'userimg',
              })
              wx.setStorage({
                data: userlist[i].username,
                key: 'username',
              })
              wx.setStorage({
                data: userlist[i].groupid,
                key: 'groupid',
              })
              users.push(userlist[i])
            }
          }

          if(users.length==0){
            wx.request({
              url: 'https://www.prebeauty.work:3001/api/userpage',
              data:{
                userid: this.data.id,
                username: e.detail.userInfo.nickName,
                userimg: e.detail.userInfo.avatarUrl,
                userbgimg:'https://www.prebeauty.work:3001/static/image/swiper1.jpg',
                groupid: 1,
                uservalue:0,
                userwords:'你的小可爱突然出现~',
                useremail:'未填写',
                userphone:'未填写',
                userbirth:'未填写',
                usercity:'未填写',
                reg_time:new Date()
              },
              header: {
                'content-type': 'application/json'
              },
              method:'POST',
              success:(res)=>{
                wx.setStorage({
                  data: e.detail.userInfo.avatarUrl,
                  key: 'userimg',
                })
                wx.setStorage({
                  data: e.detail.userInfo.nickName,
                  key: 'username',
                })
                wx.setStorage({
                  data: 1,
                  key: 'groupid',
                })
                console.log("login success!")
              }
            })

            let getvalues=this.data.getvalue
            for(let i=0;i<getvalues.length;i++){
              wx.request({
                url: 'https://www.prebeauty.work:3001/api/value',
                data:{
                  getvaluetxt:getvalues[i].getvaluetxt,
                  getvaluenum:getvalues[i].getvaluenum,
                  getvaluecan:getvalues[i].getvaluecan,
                  getvaluedid:getvalues[i].getvaluedid,
                  getvalueuser:this.data.id
                },
                header: {
                  'content-type': 'application/json'
                },
                method:'POST',
                success:(res)=>{
                  console.log("login add getvalue success!")
                }
              })
            }
            //获取所有says
            wx.request({
              url: 'https://www.prebeauty.work:3001/api/says',
              header: {
                'content-type': 'application/json'
              },
              method: 'GET',
              success:async (res)=>{
                this.setData({
                  says:res.data
                })
                let says=this.data.says
                await new Promise((rv,rj)=>{
                  for(let i=0;i<says.length;i++){
                    wx.request({
                      url: 'https://www.prebeauty.work:3001/api/like',
                      data:{
                        sayid:says[i].sayid,
                        userid:this.data.id,
                        islike:'false',
                        likeimg:'/icon/like.png'
                      },
                      header: {
                        'content-type': 'application/json'
                      },
                      method: 'POST',
                      success: (res)=>{
                        console.log('post like success!')
                        rv()
                      },
                      fail : err => {
                        rv()
                      }
                    })
                  }
                })
                
              }
            })
          }
        }
      })
    }
  }
})
